setInterval(() => {
    location.reload();
}, 3600000);

function getQueryString(name) {
    let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    let r = window.location.search.substr(1).match(reg);
    if (r != null) {
        return r[2];
    };
    return 0;
}


var checkItervalId = setInterval(()=>{
    checkPopSuccess();
},2000)

setTimeout(() => {
    clickButton();
}, 2000);
var checkItervalId_click = setInterval(()=>{
    clickButton();
},8000)


var no_pop_time = 0 ;
var all_check_times = 0;
function checkPopSuccess(){

    console.log("checkPopSuccess")
    all_check_times = 0;
    if(header_can_user===true){
        console.log("check header_can_user",header_can_user);
        return;
    }

    const high_closeBox = document.querySelector('.captcha_click_close_box');
    if (high_closeBox) {
        console.log("high_closeBox",high_closeBox);
        block();
    }

    const closeBox = document.querySelector('.captcha_canvas_close_box');
    //已经确定有滑动窗口
    if (!closeBox) {
        console.log("checkPopSuccess","no closeBox");
    }

    //已经计算好了滑动窗口需要移动多少
    if(closeBox && sm_left_px_move > 0){
        console.log("sm_left_px_move",sm_left_px_move);
        //console.log("start_move",start_move);
        if(start_move===false){
            reRryTime++;
            console.log("reRryTime",reRryTime);
            if(reRryTime>=2){
                //有可能图片识别不准确
                page_reload()
                return;
            }
            start_move = true;
            if(getQueryString("focusTab")=="true"){
                chrome.runtime.sendMessage({ data: {type:"focusTab"}, close: false });
            }
           // console.log("start_move",start_move);
            setTimeout(() => {
                simulateSlider(sm_left_px_move).then(() => {
                    console.log("滑块滑动完成！");
                    setTimeout(() => {
                        start_move = false;
                        console.log("set_start_move",start_move);
                    },1500);
                });
            },500);

        }
    }else if(closeBox && sm_left_px_move===0){
        no_pop_time++;
    }

    const goods_btn = document.querySelector('.goods-btn__add');
    console.log("set goods_btn",goods_btn);
    if(goods_btn && !closeBox){
        no_pop_time = 0;
        setTimeout(() => {
            const wrapper = document.querySelector('.sui-dialog__wrapper.sui-dialog__W996');
            if (wrapper) {
                const close_btn = wrapper.querySelector('.sui-dialog__closebtn');
                simulateClick(close_btn);
            } else {
                console.log('没有找到 class="sui-dialog__wrapper sui-dialog__W996" 的元素');
            }

        }, 1000);
        reRryTime = 0 ;
    }

    if(!closeBox && !goods_btn){
        no_pop_time++;
    }

    console.log("no_pop_time",no_pop_time);
    if(no_pop_time > 50){
        page_reload();
    }

}

var start_move = false;

var reRryTime = 0;



function clickButton(){

    console.log("clickButton")
    const closeBox = document.querySelector('.captcha_canvas_close_box');
    if (closeBox) {
        return
    }

    var button = document.querySelector('.product-card__add-btn.price-wrapper__addbag-btn');
    simulateClick(button);

}

var sm_left_px_move = 0;
var api1_fail_number = 0;
var api2_fail_number = 0;
var api3_fail_number = 0;
var api4_fail_number = 0;
var success_time = 0;

var header_quickView = "";
var header_header_static_v2 = "";
var header_header_realtime = "";
var useLocalHeader_realtime = false;
var useLocalHeader_quickview = false;
var last_fail_api_index = 0 ;
var run_index = 0;
var sm_eror = false;
var run_number = 0;
var check_pop_success = true;
var header_can_user = false;
chrome.runtime.onMessage.addListener(async (msg, sender) => {

    console.log(msg);

    if(msg.bid > 0){
        const obj = JSON.parse(msg.body);
        console.log(obj);
        /*if(msg.type==="static"){
            doWitdhDataJob(obj,msg.bid,msg.msg);
        }else if(msg.type==="realtime"){
            doWitdhDataJob(obj,msg.bid,msg.msg);
        }*/

        if(obj['code']=="200301"){
            var type = "sheindetaildel";
            var data={}
            data.id = msg.data.id;
            console.log(data);
            chrome.runtime.sendMessage({ data: {type,data }, close: false })
            setTimeout(function() {
                window.close();
            }, 10000);
        }else{
            getDataDetailApiStatic(obj,msg.bid,msg.msg.data.goods_id);
        }
    }else if(msg.type==='token_check'){

        const obj = JSON.parse(msg.body);
        let check_pic = obj['info']['validate_param']['bg_img_url'];
        console.log(obj);
        console.log(check_pic);
        check_pic = check_pic.replace("http://", "https://");
        findLargestTransparentAreaByUrl(check_pic).then(leftMostX => {
            if(leftMostX > 0){
                sm_left_px_move = leftMostX;
            }
            console.log('Left Most X:', leftMostX);
        })
            .catch(error => {
                console.error('Error:', error);
                page_reload();
            });
    }

})

function block(){
     fail_number = 0;
    let block_time = getQueryString("block");
    let block_time_int= parseInt(block_time, 10);
    if(block_time_int <=0){
        block_time_int = 180;
    }
    console.log("block_time_int",block_time_int);
     let this_lock_time = (block_time_int -5) * 1000;

    clearInterval(checkItervalId);
    clearInterval(checkItervalId_click);

    console.log("this_lock_time",this_lock_time);
    setTimeout(() => {
        location.reload()
    },this_lock_time);
}


